<?php

use App\Models\User;
use App\Models\Users;
use App\Models\WechatFans;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

function wx_config($name)
{
    $data = Cache::get($name);
    if (empty($data)) {
        //判断是否存在'.'
        if (strpos($name, '.') !== false) {
            [$name, $key] = explode('.', $name);
            $json = DB::table('configs')->where('name', $name)->value('val');
            $arr_value = json_decode($json, true);
            $data = $arr_value[$key] ?? null;
        } else {
            $json = DB::table('configs')->where('name', $name)->value('val');
            $data = json_decode($json, true);
        }

        if (!empty($data)) {
            Cache::put($name, $data);
            return $data;
        }
        return null;
    }
    return $data;
}

//生成订单号方法
function makeOrderSn()
{
    //1、生成规则
    //8（年月日）+ 6（随机数）+ 4（时间戳后四位）
    list($s1, $s2) = explode(' ', microtime());
    $second = (int)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
    return date('Ymd') . rand(100000, 999999) . substr($second, -4);
}

//获取物业端下的所有业主
function getUserIds()
{
    $estate_id = auth('estate')->user()['estate_id'];

    $user_ids = \App\Models\User::query()->where('property_id', $estate_id)->pluck('id');
    return $user_ids;
}
